#!/usr/bin/env python
# _*_coding:utf-8 _*_
__author__ = "Gao"
"""
轮询鼠标


x,y = pygame.mouse.get_pos()    # 获取鼠标当前坐标
x,y = pygame.mouse.get_rel()    # 获取位移
b1,b2,b3 = pygame.mouse.get_pressed()    # 获取鼠标按键，左键、中键、右键
pygame.mouse.set_visible(False)    # 隐藏鼠标
"""

import sys,pygame

from pygame.locals import *

def print_text(font,x,y,text,color=(255,255,255)):
    imgText = font.render(text,True,color)
    screen.blit(imgText,(x,y))

img = open('timg.jpg','rb')

pygame.init()

screen = pygame.display.set_mode((600,500))
font1 = pygame.font.Font(None,24)
white = 255,255,255
mouse_x = 0
mouse_y = 0
move_x = 0
move_y = 0

mouse_down = 0
mouse_up = 0

mouse_down_x = 0
mouse_down_y = 0
mouse_up_x = 0
mouse_up_y = 0


while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
        elif event.type == MOUSEMOTION:
            mouse_x,mouse_y = event.pos
            move_x,move_y = event.rel
        elif event.type == MOUSEBUTTONDOWN:
            mouse_down = event.button
            mouse_down_x,mouse_down_y = event.pos
        elif event.type == MOUSEBUTTONUP:
            mouse_up = event.button
            mouse_up_x, mouse_up_y = event.pos

    keys = pygame.key.get_pressed()
    if keys[K_ESCAPE]:
        sys.exit()
    screen.fill((0,100,0))
    print_text(font1,0,0,'mouse events')
    print_text(font1,0,20,'mouse position:'+ str(mouse_x)+','+str(mouse_y))
    print_text(font1,0,40,'mouse relative:'+ str(move_x)+','+str(move_y))

    print_text(font1,0,60,'mouse button down:'+ str(mouse_down)+'at'+str(mouse_down_x)+','+str(mouse_down_y))
    print_text(font1,0,80,'mouse button up:'+ str(mouse_up)+'at'+str(mouse_up_x)+','+str(mouse_up_y))

    print_text(font1, 0, 160, 'mouse polling')

    x,y = pygame.mouse.get_pos()
    print_text(font1,0,180,'mouse position:'+str(x)+','+str(y))

    b1,b2,b3 = pygame.mouse.get_pressed()
    text = ''
    for b in pygame.mouse.get_pressed():
        text +=str(b)
        text += ','
    print_text(font1,0,200,text)

    pygame.display.update()